﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//using System.Runtime.Serialization.Formatters.Binary;

using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;

namespace Sofire.DataComm.Remoting
{
    internal static class RemotingShare
    {
        public readonly static Sofire.Serialization.FastBinarySerializer Formatter = DataComm.Net.Async.AsyncSocketBase.DefaultFormatter;

#if SILVERLIGHT
        //public static readonly SharpSerializer Formatter = new SharpSerializer();

#else
        //public static readonly BinaryFormatter Formatter = new BinaryFormatter() { AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple };

        private static byte[] KeepAliveOptionValues;
        private static int _KeepAliveSeconds = 1200000;
        public static int KeepAliveSeconds
        {
            get
            {
                return _KeepAliveSeconds;
            }
            set
            {
                if(_KeepAliveSeconds != value)
                {
                    _KeepAliveSeconds = value;
                    KeepAliveOptionValues = null;
                }
            }
        }

        public static void SetKeepAlive(Socket scoket)
        {

            if(KeepAliveOptionValues == null)
            {
                uint dummy = 0;
                KeepAliveOptionValues = new byte[Marshal.SizeOf(dummy) * 3];

                //- 是否启用Keep-Alive
                BitConverter.GetBytes((uint)1).CopyTo(KeepAliveOptionValues, 0);
                //- 首次探测时间
                BitConverter.GetBytes((uint)_KeepAliveSeconds).CopyTo(KeepAliveOptionValues, Marshal.SizeOf(dummy));
                //- 间隔探测时间
                BitConverter.GetBytes((uint)_KeepAliveSeconds).CopyTo(KeepAliveOptionValues, Marshal.SizeOf(dummy) * 2);
            }

            scoket.IOControl(IOControlCode.KeepAliveValues, KeepAliveOptionValues, null);

        }
#endif
    }
}
